#include "AnimationUnit.h"

USING_NS_CC;
using namespace std;

const int gAnimationMaxFrames = 11;
const int gShortAnimation = 2;
const int gLongAnimation = 11;

const char *GDefaultAnimation = "idle";

const char* gShortAnimationList[] = {
	"idle",
};

#define ARRAY_COUNT(array) sizeof(array[0])/sizeof(array)

float getDelay(const char* animationName)
{
	string anim = animationName;
	for (int i=0;i<ARRAY_COUNT(gShortAnimationList);i++)
	{
		if (anim.find(gShortAnimationList[i]) != -1)
		{
			return 1.0f/gShortAnimation;
		}
	}

	return 1.0f/gLongAnimation;
}

string getFileName(const char* pathName)
{
	string FilePathName = pathName;
	int lastDot = FilePathName.rfind('.');
	int pos = FilePathName.rfind('/');
	int lastPos = pos == -1 ? 0 : pos;
	return FilePathName.substr(lastPos+1,lastDot-1);
}

AnimationUnit::AnimationUnit():
	mAutoIdle(true)
{
}

AnimationUnit::~AnimationUnit()
{
	for (int i=0;i<AVTAR_MAX;i++)
	{
		mAvtars[i]->release();
	}
}

bool AnimationUnit::init()
{
	for (int i=0;i<AVTAR_MAX;i++)
	{
		mAvtars[i] = CCSprite::create();
		mAvtars[i]->retain();
	}

	mCurAnimation = GDefaultAnimation;
	return true;
}

void AnimationUnit::changeAvtar(int type,const char* name)
{
	if (type<0 || type > AVTAR_MAX)
	{
		return;
	}

	if (name)
	{
		mEquipItem[type] = name;
	}
	else
	{
		mEquipItem[type] = "";
	}
	

	play(mCurAnimation.c_str());
}

CCAnimation* AnimationUnit::loadAnimation(const char* meshAnimationName)
{
	CCSpriteFrameCache *frameCache = CCSpriteFrameCache::sharedSpriteFrameCache();
	frameCache->addSpriteFramesWithFile(meshAnimationName);
	string fileName = getFileName(meshAnimationName);

	CCArray* animFrames = CCArray::createWithCapacity(gAnimationMaxFrames);
	char str[100] = {0};
	for(int i = 1; i < gAnimationMaxFrames; i++)
	{
		sprintf(str, "%s%d.png",fileName.c_str(),i);
		CCSpriteFrame *frame = frameCache->spriteFrameByName(str);
		animFrames->addObject(frame);
	}

	return CCAnimation::createWithSpriteFrames(animFrames, getDelay(meshAnimationName));
}

CCAnimation* AnimationUnit::getAnimation(const char* meshAnimationName)
{
	CCAnimation* animation = CCAnimationCache::sharedAnimationCache()->animationByName(meshAnimationName);
	if (!animation)
	{
		animation =loadAnimation(meshAnimationName);
	}

	return animation;
}

void AnimationUnit::play(const char* animationName,bool loop)
{
	for (int i=0;i<AVTAR_MAX;i++)
	{
		if (mEquipItem[i] != "")
		{
			char d = mDirection;
			bool bFlip = false;
			if (d == GToward_W)
			{
				d = GToward_E;
				bFlip = true;
			}

			std::string animationName = d + mEquipItem[i] + std::string("_") + animationName;
			CCAnimation* animation = getAnimation(animationName.c_str());		

			if (loop == false)
			{
				CCAnimate * animate = CCAnimate::create(animation);
				CCSequence *seq = CCSequence::create(animate, CCCallFunc::create(this,callfunc_selector(AnimationUnit::finish)));
				mAvtars[i]->runAction(seq);
			}
			else
			{
				CCAnimate * animate = CCAnimate::create(animation);
				mAvtars[i]->runAction(CCRepeatForever::create(animate));
			}
			
			mAvtars[i]->setFlipX(bFlip);
		}
	}
}

void AnimationUnit::finish()
{
	play(GDefaultAnimation,true);
}